package com.struts.interceptor;


import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.alibaba.fastjson.JSONObject;
import com.struts.context.ActionContext;
import com.struts.invocation.InvocationWrap;
import com.struts.stack.ValueStack;

public class ParametersInterceptor implements Interceptor {

	@Override
	public void init() {

	}

	@Override
	public String intercept(InvocationWrap invocation) {
		ActionContext ac = invocation.getInvocationContext();
		ValueStack valueStack = ac.getValueStack();
		// 获取action对象
		Object action = valueStack.peek();
		try {
			Map map = new HashMap();
			Iterator itr = ac.getRequest().getParameterMap().keySet().iterator();
			while(itr.hasNext()) {
				String name = (String) itr.next();
				String value = ac.getRequest().getParameter(name);
				map.put(name, value);
			}
			
			BeanUtils.populate(action, map);
			return invocation.invoke(invocation);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void destory() {

	}

}
